﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Util;
using Newtonsoft;
using LeSoft.Application.Entity;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using System.Threading;

namespace LeSoft.Application.Web.Areas.ResourceManage.Controllers
{
    public class PStructColorController : MvcControllerBase
    {
        //
        // GET: /ResourceManage/PStructColor/

        private PStructColorBLL Bll = new PStructColorBLL();
        private PStructCache pstructCache = new PStructCache();
        private RoomCache roomcache = new RoomCache();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "房房态首页", "Index")]
        public ActionResult Index()
        {

            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "房态颜色", "Form")]
        public ActionResult Form()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "房态信息", "PSForm")]
        public ActionResult PSForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 
        /// </summary>
        /// <param name="OrganizeId"></param>
        /// <returns></returns>
        public ActionResult GetRoomTreeJson(string OrganizeId)
        {
            var pstructdata = new PStructBLL().GetList().OrderBy(t => t.Code).ToList();
            var treeList = new List<TreeEntity>();

            if (!string.IsNullOrEmpty(OrganizeId))
            {
                pstructdata = pstructdata.Where(t => t.OrganizeId == OrganizeId).ToList();
            }
            if (pstructdata != null)
            {
                foreach (PStructEntity item in pstructdata)
                {
                    #region 房产
                    if (item.Type <= 2)
                    {
                        TreeEntity tree = new TreeEntity();
                        bool hasChildren = pstructdata.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                        tree.id = item.Id;
                        tree.text = item.Name;
                        tree.value = item.AllName;
                        tree.title = item.AllCode;
                        tree.Attribute = "PSAllName";
                        tree.AttributeValue = item.AllName;
                        tree.parentId = item.ParentId;
                        tree.AttributeA = "Type";
                        tree.AttributeValueA = item.Type.ToString();
                        tree.isexpand = false;
                        tree.complete = true;
                        tree.hasChildren = hasChildren;
                        switch (item.Type)
                        {
                            case 1: tree.img = "fa fa-university"; break;
                            case 2: tree.img = "fa fa-building-o"; break;
                            default:
                                tree.img = "fa fa-th-list";
                                break;
                        }
                        if (item.ActiveMark == 1)
                            tree.imgstyle = "color:#2df12d";
                        treeList.Add(tree);
                    }

                    #endregion
                }
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 获取房态信息
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "房间信息", "GetPSInfo")]
        public ActionResult GetPSInfo(string keyValue)
        {
            var data = Bll.GetPSInfo(keyValue);
            return Content(data);
        }

        /// <summary>
        /// 获取房间颜色集合
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns> 
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "房间集合", "GetData")]
        public ActionResult GetData(string keyValue)
        {

            var pstructdata = Bll.GetPSList(keyValue).OrderBy(t => t.Code).ToList();
            var data1 = Bll.GetRoomList(keyValue);
            List<RoomEntity> lst = ConvertHelper<RoomEntity>.DataTableToIList(data1);
            var lst1 = pstructdata.Where(t => t.Type == 1).ToList(); //楼盘
            var lst2 = pstructdata.Where(t => t.Type == 2).ToList();//楼阁
            var lst3 = pstructdata.Where(t => t.Type == 3).ToList();//楼栋
            var lst4 = pstructdata.Where(t => t.Type == 4).ToList();//楼层
             
            var lst5 = pstructdata.Where(t => t.Type == 5).ToList();//房间  
            foreach (var item in lst5)
            {
                RoomEntity room = lst.Where(e => e.PStructId == item.Id).FirstOrDefault();
                item.IsPublish = (room == null || room.State == null) ? 0 : room.State;
            }
            PStructEntity entity = pstructdata.FirstOrDefault(t => t.Id == keyValue);
            int Count = lst2.Count(e => e.ParentId == entity.Id);
            var data = new
            {
                Lst1 = lst1,
                Lst2 = lst2,
                Lst3 = lst3,
                Lst4 = lst4,
                Lst5 = lst5,
                Entity = entity,
                Count = Count
            };
            return Content(data.ToJson());
        }

        /// <summary>
        /// 获取统计数据
        /// </summary>
        /// <returns></returns> 
        [HttpPost] 
        public ActionResult GetScale()
        {
            List<RoomEntity> lst = roomcache.GetList().ToList();
            //lst = lst.Where(e => e.SplitedMark != 1 && e.EnabledMark == 1).ToList();
            lst = lst.Where(e => e.EnabledMark == 1).ToList();
            var query = Bll.GetList();
            query = query.Where(e => e.State != 4 && e.State != 5);
            string html = "<table>";
            html += "<tr style='background-color:#CDCDCD'><th>状态</th><th>数量</th><th>比例</th></tr>";
            foreach (var item in query)
            {
                if (lst.Count <= 0)
                {
                    html += "<tr style='background-color:" + item.Color + "'><td>" + item.StateName + "</td><td>" + lst.Count(e => e.State == item.State) + "</td><td>0.00%</td></tr>";

                }
                else
                {
                    html += "<tr style='background-color:" + item.Color + "'><td>" + item.StateName + "</td><td>" + lst.Count(e => e.State == item.State) + "</td><td>" + Convert.ToDouble(Convert.ToDouble(lst.Count(e => e.State == item.State)) / Convert.ToDouble(lst.Count())).ToString("0.00%") + "</td></tr>";

                }

            }
            html += "<tr style='background-color:#F5D379'><td>小计</td><td>" + lst.Count() + "</td><td>100.00%</td></tr>";
            html += "</table>";
            return Json(html);

        }
       [HttpPost] 
        public ActionResult GetScaleByPSId(string keyValue)
        {
            var data = Bll.GetRoomList(keyValue);
            var lst = ConvertHelper<RoomEntity>.DataTableToIList(data);
            lst = lst.Where(e => e.SplitedMark != 1 && e.EnabledMark == 1).ToList();
            var query = Bll.GetList();
            query = query.Where(e => e.State != 4 && e.State != 5);
            string html = "<table>";
            html += "<tr style='background-color:#CDCDCD'><th>状态</th><th>数量</th><th>比例</th></tr>";
            foreach (var item in query)
            {
                if (lst.Count <= 0)
                {
                    html += "<tr style='background-color:" + item.Color + "'><td>" + item.StateName + "</td><td>" + lst.Count(e => e.State == item.State) + "</td><td>0.00%</td></tr>";
                }
                else
                {
                    html += "<tr style='background-color:" + item.Color + "'><td>" + item.StateName + "</td><td>" + lst.Count(e => e.State == item.State) + "</td><td>" + Convert.ToDouble(Convert.ToDouble(lst.Count(e => e.State == item.State)) / Convert.ToDouble(lst.Count())).ToString("0.00%") + "</td></tr>";

                }
            }
            html += "<tr style='background-color:#F5D379'><td>小计</td><td>" + lst.Count() + "</td><td>100.00%</td></tr>";

            html += "</table>";
            return Json(html);
        }
        /// <summary>
        /// 获取颜色配置集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "状态颜色数据列表", "GerColorList")]
        public ActionResult GerColorList()
        {
            var data = Bll.GetList();
            data = data.Where(e => e.State != 4 && e.State != 5);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 生成默认颜色配置
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDefaultColor()
        {
            try
            {
                Bll.SaveColor("");
                var data = Bll.GetList();
                return Content(data.ToJson());
            }
            catch (Exception)
            {

                return Error("默认颜色配置生成失败");
            }
        }

        /// <summary>
        /// 获取房产全名
        /// </summary>
        /// <param name="keyValue">房产Id</param>
        /// <returns></returns>
        public ActionResult GetAllName(string keyValue)
        {
            var lst = pstructCache.GetList().ToList();
            PStructEntity entity = lst.Where(e => e.Id == keyValue).FirstOrDefault();
            string AllName = "";
            if (entity != null)
            {
                AllName += entity.Name;
                PStructEntity entity1 = new PStructBLL().GetEntity(entity.ParentId);
                if (entity1 != null)
                {
                    AllName = entity1.Name + "/" + AllName;
                }

            }
            return Json(AllName);
        }

        #endregion

        #region 保存数据
        public ActionResult SaveColor(string queryJson)
        {
            string Json = HttpUtility.UrlDecode(queryJson);
            try
            {
                Bll.SaveColor(Json);
                return Success("保存成功");
            }
            catch (Exception)
            {
                return Error("保存失败");
            }
        }
        #endregion

    }
}
